package com.xbongbong.paas.util;

import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.pojo.SummaryDataPoJo;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2020/6/11 13:49
 * @since v1.0
 */
public class FormUtil {

    /**
     * 是否有业务规则使用权限
     * @param businessType
     * @param saasMark
     * @return true：有 false ： 没有
     */
    public static Boolean haveUserPermission(Integer businessType, Integer saasMark, Integer distributorMark) {
        if (Objects.equals(SaasMarkEnum.PAAS.getCode(), saasMark)) {
            return true;
        }
        // https://xbb.yuque.com/lfwuxq/vsf9sv/tgtiwr
        // crm： 客户、联系人、跟进记录，机会，退货退款，合同，报价单，竞争对手
        // 进销存：供应商，供应商联系人，供应商跟进记录，采购合同，采购退货，装配单，盘点单，调拨单，生产单，bom单，采购入库，生产退料单，销售退货入库单、生产入库单、其他入库单、销售出库，其他出库，采购退货出库单，生产领料单，仓库
        // 资金：回款单，付款单，进项发票，销项发票
        // 市场管理：线索，市场活动
        // 工单
        List<Integer> excludeBusinessType = Arrays.asList(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), XbbRefTypeEnum.CONTACT.getCode(),XbbRefTypeEnum.CUSTOMER_COMMUNICATE.getCode(),
                XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), XbbRefTypeEnum.REFUND.getCode(), XbbRefTypeEnum.CONTRACT.getCode(),
                XbbRefTypeEnum.QUOTATION.getCode(), XbbRefTypeEnum.SUPPLIER.getCode(), XbbRefTypeEnum.SUPPLIER_CONTACT.getCode(), XbbRefTypeEnum.SUPPLIER_COMMUNICATE.getCode(),
                XbbRefTypeEnum.PURCHASE.getCode(), XbbRefTypeEnum.RETURNED_PURCHASE.getCode(), XbbRefTypeEnum.ASSEMBLE.getCode(), XbbRefTypeEnum.INVENTORY.getCode(),
                XbbRefTypeEnum.TRANSFER.getCode(), XbbRefTypeEnum.PRODUCTION_ORDER.getCode(), XbbRefTypeEnum.BOM_BILL.getCode(), XbbRefTypeEnum.PURCHASE_INSTOCK.getCode(),
                XbbRefTypeEnum.RETURNED_MATERIEL_INSTOCK.getCode(), XbbRefTypeEnum.REFUND_INSTOCK.getCode(), XbbRefTypeEnum.PRODUCTION_INSTOCK.getCode(), XbbRefTypeEnum.OTHER_INSTOCK.getCode(),
                XbbRefTypeEnum.CONTRACT_OUTSTOCK.getCode(), XbbRefTypeEnum.OTHER_OUTSTOCK.getCode(), XbbRefTypeEnum.RETURNED_PURCHASE_OUTSTOCK.getCode(),
                XbbRefTypeEnum.PRODUCTION_MATERIEL_OUTSTOCK.getCode(), XbbRefTypeEnum.WAREHOUSE.getCode(),
                XbbRefTypeEnum.PAYMENT_SHEET.getCode(), XbbRefTypeEnum.PAY_SHEET.getCode(), XbbRefTypeEnum.INVOICE.getCode(), XbbRefTypeEnum.PURCHASE_INVOICE.getCode(), XbbRefTypeEnum.CLUE.getCode(), XbbRefTypeEnum.MARKET_ACTIVITY.getCode(),
                XbbRefTypeEnum.WORK_ORDER.getCode(),XbbRefTypeEnum.COMPETITOR.getCode()
                );
        if (Objects.nonNull(businessType) && excludeBusinessType.contains(businessType) && !Objects.equals(DistributorMarkEnum.DISTRIBUTOR.getCode(), distributorMark)) {
            return true;
        }
        return false;
    }

    /**
     * 是否有工作流权限
     * @param businessType
     * @param saasMark
     * @param distributorMark
     * @return true：有 false ： 没有
     */
    public static Boolean haveWorkflowPermission(Integer businessType, Integer saasMark, Integer distributorMark) {
        if (Objects.equals(SaasMarkEnum.PAAS.getCode(), saasMark)) {
            return true;
        }
        // https://xbb.yuque.com/lfwuxq/vsf9sv/tgtiwr
        // crm： 客户、联系人、跟进记录，机会，退货退款，合同，报价单
        // 进销存：供应商，供应商联系人，供应商跟进记录，采购合同，采购退货，装配单，盘点单，调拨单，生产单，bom单，采购入库，生产退料单，销售退货入库单、生产入库单、其他入库单、销售出库，其他出库，采购退货出库单，生产领料单，仓库
        // 资金：回款单，付款单，进项发票，销项发票
        // 市场管理：线索，市场活动
        // 工单
        List<Integer> excludeBusinessType = Arrays.asList( XbbRefTypeEnum.WORK_ORDER_V2.getCode(), XbbRefTypeEnum.RECEIPT_ORDER.getCode(), XbbRefTypeEnum.SERVICE_PROJECT.getCode()
        );
        if (Objects.nonNull(businessType) && excludeBusinessType.contains(businessType) && !Objects.equals(DistributorMarkEnum.DISTRIBUTOR.getCode(), distributorMark)) {
            return true;
        }
        return haveUserPermission(businessType, saasMark, distributorMark);
    }



    public static boolean sameForm(Long formId, Long targetFormId, Integer businessType, Integer targetBusinessType) {
        return Objects.equals(formId, targetFormId) && Objects.equals(businessType, targetBusinessType);
    }


    public static void handleFormEntity(PaasFormEntityExt paasFormEntityExt, Integer saasMark, Integer businessType) {
        if (Objects.equals(saasMark, SaasMarkEnum.PAAS.getCode())) {
            return;
        }

        //由于菜单名称可以自定义，因此只过滤默认名称中的“管理”
        String name = paasFormEntityExt.getName();
        name = name.replaceAll("管理","");
        paasFormEntityExt.setName(name);
    }

    /**
     * 通过摘要字段获取摘要内容
     * 重构前 -> com.xbongbong.saas.service.impl.MobileDetailServiceImpl#getSummaryDataPoJoByAttr(java.util.List, java.lang.String)
     * @param summaryList
     * @param attr
     * @return
     */
    public static SummaryDataPoJo getSummaryDataPoJoByAttr(List<SummaryDataPoJo> summaryList, String attr) {
        for (SummaryDataPoJo summaryItem : summaryList) {
            if (Objects.equals(summaryItem.getAttr(), attr)) {
                return summaryItem;
            }
        }
        return null;
    }
}
